﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Composite.Presentation.Regions;
using System.Windows.Controls;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Events;
using System.Windows.Forms.Integration;

namespace Infrastructure.WpfClient.Composite.Regions.FrameRegion
{
    public class FrameRegionAdapter: RegionAdapterBase<Frame>
    {
        [Dependency]
        public IUnityContainer Container { get; set; }
        public FrameRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {
        
        }
        
        protected override Microsoft.Practices.Composite.Regions.IRegion CreateRegion()
        {
            return Container.Resolve<FrameViewRegion>();
        }
        protected override void Adapt(Microsoft.Practices.Composite.Regions.IRegion region, Frame regionTarget)
        {         
            region.ActiveViews.CollectionChanged += (sender, e) =>
            {
                switch (e.Action)
                {
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                        var view = region.ActiveViews.FirstOrDefault();
                        if (view != null)
                        {                        
                            if ( view is Uri)
                                regionTarget.NavigationService.Navigate((Uri)view);
                            else if (view is Infrastructure.WpfClient.Views.Master.MasterPresenterController)
                            {
                                var presenter = view as Infrastructure.WpfClient.Views.Master.MasterPresenterController;
                                var nav = regionTarget.NavigationService;
                                presenter.NavigationService = nav as System.Windows.Navigation.NavigationService;
                                presenter.Run();
                            }                   
                            else
                                regionTarget.NavigationService.Navigate(view);

                            regionTarget.Visibility = System.Windows.Visibility.Visible;
                        }                            
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                        
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Replace:
                        break;
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Reset:
                        while (regionTarget.NavigationService.CanGoBack)
                        {
                            regionTarget.NavigationService.RemoveBackEntry();
                        }                      
                        regionTarget.Visibility = System.Windows.Visibility.Collapsed;
                        break;
                    default:
                        break;
                }
                
            };
            
            region.Views.CollectionChanged += (sender , e)=>
            {
                switch (e.Action)
                {
                    case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                        region.Activate(e.NewItems[0]);
                    break;                                        
                }
            };
        }
    }
}
